const is = (tokens) => {
  return tokens.peek().type === 'keyword' && tokens.peek().value === 'import'
}

const parse = (tokens) => {
  const node = {
    type: 'ImportDeclaration',
    source: [],
    alias: ''
  }

  tokens.next()

  if (tokens.peek().type !== 'identifier') {
    tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
  }

  node.source.push({
    type: 'Identifier',
    value: tokens.peek().value
  })

  tokens.next()

  while (!tokens.eof() && (tokens.peek().type === 'dot' && tokens.peek().value === '.')) {
    tokens.next()

    if (tokens.peek().type !== 'identifier') {
      tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
    }

    node.source.push({
      type: 'identifier',
      value: tokens.peek().value
    })

    tokens.next()
  }

  if (tokens.peek().type === 'keyword' && tokens.peek().value === 'as') {
    tokens.next()

    if (tokens.peek().type !== 'identifier') {
      tokens.croak(`Can't handle token: ${ tokens.peek().value }`)
    }

    node.alias = tokens.peek().value

    tokens.next()
  }

  return node
}

export default {
  is,
  parse
}
